<?php


namespace Biz\model\brand;


use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\IdParse;
use Encore\Admin\Facades\Admin;

class BrandCollege extends BaseDbModel
{

    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_college';

    /**
     * @desc 获取品牌商学院obj列表
     * @return array|bool
     */
    public static function getList()
    {
        $where = array(
            'data_status' => self::STATUS_NORMAL,
        );
        $where['ORDER'] = ['weight' => 'ASC'];
        return self::select('*', $where);
    }

    /**
     * @desc 新增obj
     *
     * @param $objId
     *
     * @return bool
     */
    public static function insterObj($objId)
    {
        $data = array(
            'obj_id'      => $objId,
            'addtime'     => time(),
            'edittime'    => time(),
            'data_status' => self::STATUS_NORMAL,
            'operator_id' => Admin::user()->id,
            'obj_type'    => IdParse::getType($objId),
            'weight'      => self::max('weight') + 1
        );
        $result = self::insert($data);

        if ($result->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    /**
     * @desc 更新obj
     *
     * @param $id
     * @param $objId
     *
     * @return bool
     */
    public static function updateObj($id, $objId)
    {
        $data = array(
            'obj_id'      => $objId,
            'edittime'    => time(),
            'operator_id' => Admin::user()->id,
            'obj_type'    => IdParse::getType($objId)
        );
        $result = self::update($data, ['id' => $id]);
        if ($result->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    /**
     * @desc 删除obj
     *
     * @param $id
     *
     * @return bool
     */
    public static function deleteObj($id)
    {
        $data = array(
            'edittime'    => time(),
            'operator_id' => Admin::user()->id,
            'data_status' => self::STATUS_DELETE,
            'weight'      => 99
        );
        $result = self::update($data, ['id' => $id]);
        if ($result->errorCode() != '00000') {
            return false;
        }
        return true;
    }

    /**
     * @desc 批量修改排序权重
     *
     * @param $sort
     */
    public static function weight($sort)
    {
        foreach ($sort as $key => $value) {
            self::update(['weight' => $value], ['id' => $key]);
        }
        return true;
    }

    /**
     * @desc 获取按权重排序objs
     * @return array|bool
     */
    public static function getObjsBySort()
    {
        $where = [
            'data_status' => self::STATUS_NORMAL,
        ];
        $where['ORDER'] = ['weight' => 'ASC'];
        return self::select('obj_id', $where);
    }

    /**
     * @desc 获取指定数量的obj
     * @return array|bool
     */
    public static function getObjIds($count)
    {
        $where = [
            'data_status' => self::STATUS_NORMAL,
        ];
        $where['ORDER'] = ['weight' => 'ASC'];
        $where['LIMIT'] = [0, $count];
        return self::select('obj_id', $where);
    }

    /**
     * @desc 获取指定数量的obj
     * @return array|bool
     */
    public static function getPagedObjIds($p, $count = 20)
    {
        $where = [
            'data_status' => self::STATUS_NORMAL,
        ];
        $where['ORDER'] = ['weight' => 'ASC'];
        $where['LIMIT'] = [($p - 1) * $count, $count];
        return self::select('obj_id', $where);
    }

    //获取Obj数量
    public static function getObjsCount()
    {
        $where = [
            'data_status' => self::STATUS_NORMAL,
        ];
        return self::count($where);
    }

    public static function getObj($objId)
    {
        $where = [
            'data_status' => self::STATUS_NORMAL,
            'obj_id'      => $objId,
        ];
        return self::get('*', $where);
    }
}